<template>
  <div>
    <!-- 表单 -->
    <el-form
      ref="formData"
      label-width="120px"
      :rules="rules"
      :model="formData"
    >
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          style="width: 50%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="formData.mobile"
          style="width: 50%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          style="width: 50%"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          style="width: 50%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          /> </el-select>==={{ formData.formOfEmployment }}
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          style="width: 50%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          style="width: 50%"
          placeholder="请选择部门"
          @focus="showTree = true"
        />
        <el-tree
          v-if="showTree"
          :data="data"
          :props="defaultProps"
          default-expand-all
          @node-click="handleNodeClick"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          style="width: 50%"
          placeholder="请选择转正时间"
        />
      </el-form-item>
      <el-form-item>
        <el-button>取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import EmployeeEnum from '@/constant/employees'
import { getDepartments } from '@/api/departments'
import arrayToTree from 'array-to-tree'
import { addEmployee } from '@/api/employees.js'
export default {
  name: '',
  data() {
    return {
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      hireType: EmployeeEnum.hireType,
      /* 部门 */
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showTree: false,
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getDep()
  },
  methods: {
    async getDep() {
      /*
      展示数据列表
      1-封装api接口请求函数 api/departments
      2-需要调用封装好的接口函数
        2-1 导入api函数  departments.vue
        2-2 调用  getDep()中调用
        2-3 成功后可以获取组织架构数据列表数组，直接赋值
        2-4 修改渲染的字段   结构上：data.name
        2-5 遗留了一个问题：没有呈现树形结构？
       */
      const res = await getDepartments()
      // this.data = res.data.depts
      this.data = arrayToTree(res.data.depts, {
        parentProperty: 'pid',
        customID: 'id'
      })

      // this.data = toDo2(res.data.depts, '')
      console.log(this.data, 6665)
    },

    /* 部门 */
    handleNodeClick(data) {
      /*
      步骤：
      1-显示树形控件
      2-点击树形获取数据显示在表单中
      */
      console.log(data, 7777)
      //  只有点击了最后一级才能关闭树形控件
      if (!data.children) {
        // 关闭控件
        this.showTree = false
        // 将选中的数据显示到表单
        this.formData.departmentName = data.name
      }
    },

    /* 点击对话框确认按钮 */
    submit() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          // alert(1111)
          this.doAdd() //  实现员工添加
        }
      })
    },

    async doAdd() {
      // 添加员工的请求
      const res = await addEmployee(this.formData)
      console.log(res, 555666)
      //  关闭对话框+刷新页面的回调
      this.$emit('success')
      this.close()
    },
    close() {
      this.formData = {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      }
    }
  }
}
</script>
<style lang="less" scoped></style>
